package com.qf.action;

import com.qf.dto.DqAlmusDTO;
import com.qf.dto.DqGroundsDTO;
import com.qf.dto.ResultDTO;
import com.qf.dto.UploadDTO;
import com.qf.service.DqAlmusService;
import com.qf.service.DqGroundsService;
import com.qf.service.UpLoadService;
import com.qf.utils.DateUtils;
import com.qf.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;

/**
 * //                       .::::.
 * //                     .::::::::.
 * //                    :::::::::::  come on
 * //                 ..:::::::::::'
 * //              '::::::::::::'
 * //                .::::::::::
 * //           '::::::::::::::..
 * //                ..::::::::::::.
 * //              ``::::::::::::::::
 * //               ::::``:::::::::'        .:::.
 * //              ::::'   ':::::'       .::::::::.
 * //            .::::'      ::::     .:::::::'::::.
 * //           .:::'       :::::  .:::::::::' ':::::.
 * //          .::'        :::::.:::::::::'      ':::::.
 * //         .::'         ::::::::::::::'         ``::::.
 * //     ...:::           ::::::::::::'              ``::.
 * //    ```` ':.          ':::::::::'                  ::::..
 * //                       '.:::::'                    ':'````..
 * //
 *
 * @author:小川
 * @date: 2018/11/24 11:44
 */
@Controller
public class UpLoadAction {
    @Autowired
    private DqAlmusService dqAlmusService;

    @Autowired
    private DqGroundsService dqGroundsService;

    @Autowired
    private UpLoadService upLoadService;
    @RequestMapping(value = "/pic/upload",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String upload(@RequestParam("uploadFile")MultipartFile multipartFile) throws Exception {
        UploadDTO upload = upLoadService.upload(multipartFile);

        String s = JsonUtils.objectToJson(upload);

        return s;
    }
    @RequestMapping("/item/groundsave")
    @ResponseBody
    public ResultDTO save(HttpServletRequest request) throws ParseException {
        String entryid = request.getParameter("entryid");
        String name = request.getParameter("name");
        String image = request.getParameter("image");
        String description = request.getParameter("description");
        DqGroundsDTO dqGroundsDTO=new DqGroundsDTO();
        dqGroundsDTO.setDepotEntryId(Integer.parseInt(entryid));
        dqGroundsDTO.setName(name);
        dqGroundsDTO.setDescription(description);
        dqGroundsDTO.setCreatedAt(DateUtils.dateToString(new Date()));
        dqGroundsDTO.setUpdatedAt(DateUtils.dateToString(new Date()));
        int groundId = dqGroundsService.insert(dqGroundsDTO);

        if(!StringUtils.isEmpty(image)){
            DqAlmusDTO dqAlmusDTO=new DqAlmusDTO();
            dqAlmusDTO.setSourceId(groundId);
            dqAlmusDTO.setDescription(description);
            dqAlmusDTO.setName(name);
            dqAlmusDTO.setPictureUrl(image);
            dqAlmusDTO.setCreatedAt(DateUtils.dateToString(new Date()));
            dqAlmusDTO.setUpdatedAt(DateUtils.dateToString(new Date()));
            dqAlmusService.addAlmus(dqAlmusDTO);
        }
        return ResultDTO.ok();
    }
}
